import * as _ from "lodash"
import httpClient from "../httpclient"

export class AuthorityDescription {
  public authority: string
  public language: string
  public name: string
  public description: string
  public children: AuthorityDescription[] = []
  public position: string
}

export class AuthorityDescriptionId {
  public authority: string
  public language?: string
}

function assignAuthorityDescription(description: AuthorityDescription) {
  return _.assign(new AuthorityDescription(), description, {
    children: _.map(description.children, assignAuthorityDescription),
  })
}

export class AuthorityServiceV1 {
  public static getAuthorityDescription(id: AuthorityDescriptionId): Promise<AuthorityDescription> {
    return httpClient.get(`/v1/authorities/${id.authority}/description`)
      .then(({ data }) => data)
      .then(assignAuthorityDescription)
  }
}
